package com.mio.scalademo

class UserTime() {
  var name: String=null
  var location: String=null
  var starttime: Int =  _
  var staytime: Int =  _
  def this(name: String, location: String,starttime: Int, staytime: Int) = {
      this( )
      this.name = name
      this.location = location
      this.starttime = starttime
      this.staytime = staytime
  }
//
}

object UserTime {
  def main(args: Array[String]): Unit = {
    val data1 = new UserTime("UserA","LocationA",8,60)
    val data2 = new UserTime("UserA","LocationA",9,60)
    val data3 = new UserTime("UserB","LocationA",10,60)
    val data4 = new UserTime("UserB","LocationA",11,80)
    val list: List[UserTime] = List(data1, data2 ,data3,data4)
    val userMap = scala.collection.mutable.Map[String,UserTime]()
    
//    list.foreach(print _)
    for (u <- list) {
      var key = u.name + u.location
      if(!userMap.contains(key)) {
        userMap(key) = u
      }else {
        var user:UserTime = userMap(key)
        //开始时间取最早时间，停留时长累计求和
        user.staytime =user.staytime  + u.staytime 
        if(u.starttime < user.starttime){
          user.starttime = u.starttime
        }
        userMap(key) = user
      }
    }
    
    for (key <- userMap.keys) {
      var user:UserTime = userMap(key)
      println("key is: " + key + " ,starttime is: " + user.starttime+ " ,staytime is: " + user.staytime)
    }
  }
}